package com.txsj.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.txsj.scan.R;
import com.txsj.scan.adapter.EpcAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.base.IView;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.utils.glide.GlideImgManager;
import com.txsj.scan.widget.TitleBarView;
import com.txsj.scan.widget.dialog.LabelIntroduceDialog;

import java.io.Serializable;

/**
 * Created by wangchuangjun on 2018/12/12
 * Desc: 已绑定商品标签详情
 */

public class BindLabelActivity extends BaseActivity<BasePresenter> implements IView, EpcAdapter.IOnItemClickListener {

    ImageView iv_goods_img;
    TextView tv_goods_name;
    TextView tv_goods_num; //商品条码
    RecyclerView recyclerView;
    EpcAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_label);
        initView();
        initData();
    }

    GoodsInfo goodsInfo;

    private void initData() {
        if (null == getIntent() || null == getIntent().getStringExtra("data")) return;
        String json = getIntent().getStringExtra("data");
        goodsInfo = new Gson().fromJson(json, GoodsInfo.class);
        GlideImgManager.getLoadImg(this, goodsInfo.getGoods_img(), iv_goods_img);
        tv_goods_name.setText(goodsInfo.getGoods_name());
        tv_goods_num.setText(goodsInfo.getGoods_num());
        if (null != goodsInfo.getEpcList() && goodsInfo.getEpcList().size() > 0) {
            adapter.setList(goodsInfo.getEpcList());
        }
    }

    private void initView() {
        mTitle = (TitleBarView) findViewById(R.id.mTitle);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        tv_goods_name = (TextView) findViewById(R.id.tv_goods_name);
        tv_goods_num = (TextView) findViewById(R.id.tv_goods_num);
        iv_goods_img = (ImageView) findViewById(R.id.iv_goods_img);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new EpcAdapter(this);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        mTitle.setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new LabelIntroduceDialog(BindLabelActivity.this).show();
            }
        });

        mTitle.setLeftOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("epcData", new Gson().toJson(goodsInfo));
                setResult(101, intent);
                finish();
            }
        });
    }

    @Override
    protected BasePresenter providePresenter() {
        return new BasePresenter(this) {
            @Override
            protected void initModel() {

            }
        };
    }

    @Override
    public Context context() {
        return this;
    }

    @Override
    public void onDelete(int position) {
        goodsInfo.getEpcList().remove(position);
        adapter.notifyDataSetChanged();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent();
            intent.putExtra("epcData", new Gson().toJson(goodsInfo));
            setResult(101, intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected boolean enableSliding() {
        return false;
    }
}
